
INS_DIR	:= ../../bin/

CFLAGS 		+= -Wall -fPIC -Werror -g -w -DDEBUG=1 -fmessage-length=0 -O2
CFLAGS		+= -I../include

CXXFLAGS	= $(CFLAGS)

APLDFLAGS	:= -L$(INS_DIR)
LDFLAGS		+= -Wl,--as-needed -lz

LIB_SRCS := \
	backed_block.c \
	output_file.c \
	sparse.c \
	sparse_crc32.c \
	sparse_err.c \
	sparse_read.c

AP1_SRC := simg2img.c sparse_crc32.c
AP2_SRC := img2simg.c

LIB_OBJS := $(patsubst %.c,%.o,$(LIB_SRCS))
AP1_OBJS := $(patsubst %.c,%.o,$(AP1_SRC))
AP2_OBJS := $(patsubst %.c,%.o,$(AP2_SRC))

ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
LIB	:= libsparse.so
APLDFLAGS	+= -lsparse
LDFLAGS		+= -Wl,-rpath=.:./bin
else
LIB	:= libsparse.a
APLDFLAGS	+= -Wl,-Bstatic -lsparse -Wl,-Bdynamic
endif

AP1	:= simg2img
AP2	:= img2simg

TARGET	:= $(LIB) $(AP1) $(AP2)

all:$(TARGET)
	
$(LIB):$(LIB_OBJS)
ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
	$(CC) -o $@ $^ -shared $(LDFLAGS)
	$(STRIP) $@
else
	$(AR) rc $@ $^
endif
	install -m 755 $@ $(INS_DIR)

$(AP1):$(AP1_OBJS)
	$(CC) -o $@ $^ $(APLDFLAGS) $(LDFLAGS)
	$(STRIP) $@

$(AP2):$(AP2_OBJS)
	$(CC) -o $@ $^ $(APLDFLAGS) $(LDFLAGS)
	$(STRIP) $@

%.o:%.c
	$(CC) $(CFLAGS) -c -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o $@ $<

%.o:%.cpp
	$(CXX) $(CXXFLAGS) -c -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o $@ $<

install:
	test -d $(INS_DIR) || install -m 755 -d $(LIBDIR)
	install -m 755 $(AP1) $(INS_DIR)
	install -m 755 $(AP2) $(INS_DIR)

clean:
	-rm -rf $(LIB_OBJS) $(AP1_OBJS) $(AP2_OBJS) *.d $(TARGET)

.PHONY: all clean install
	